package com.scommunity.admin.controller;

import com.scommunity.common.ResponseResult;
import com.scommunity.domain.Notice;
import com.scommunity.service.INoticeService;
import com.scommunity.utils.RedisUtils;
import com.scommunity.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/notice")
public class NoticeController{

    @Autowired
    private INoticeService noticeService;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("/list")
    public ResponseResult list(){
        List<Notice> noticeList = noticeService.getList();
        return ResponseResult.success(noticeList);
    }

    @PostMapping("/add")
    public ResponseResult add(@RequestBody Notice notice){
        notice.setNoticeId(snowflakeIdWorker.nextId());
        notice.setNoticeTime(LocalDateTime.now());
        List<Notice> noticeList = noticeService.list(null);
        redisUtils.set("noticeList",noticeList);
        noticeService.save(notice);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody Notice notice){
        noticeService.update(notice);
//        noticeService.updateById(notice);
        return ResponseResult.success();
    }

    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody Long noticeId){
        noticeService.delete(noticeId);
        return ResponseResult.success();
    }


}
